import { Component, ViewChild, OnInit } from '@angular/core';
import { STComponent, STColumn, STColumnTag, STColumnBadge } from '@delon/abc';
import { MsgMetaModalComponent } from './edit/msg-meta-modal.component';
import { _HttpClient, ModalHelper, SettingsService } from '@delon/theme';
import { NzNotificationService } from 'ng-zorro-antd';
import { StragegySelectModalComponent } from '../components/strategy/strategy-modal.component';

const BADGE_MSGTYPE: STColumnTag = {
  SYS: { text: '系统', color: 'red' },
  USER: { text: '用户', color: 'blue' },
};

const BADGE_STATUS: STColumnBadge = {
  '0': { text: '正常', color: 'success' },
  '1': { text: '冻结', color: 'error' },
};

const BADGE_BROADCAST: STColumnBadge = {
  '0': { text: '否', color: 'default' },
  '1': { text: '是', color: 'error' },
};

/**
 * 消息分类
 */
@Component({
  selector: 'web-message-meta',
  templateUrl: './msg-meta.component.html',
})
export class MsgMetaComponent implements OnInit {
  url = `/cf/admin/web-msg/meta/page`;
  params = { msgName: '' };
  auth_btn_add = false;
  auth_btn_edit = false;
  auth_btn_del = false;
  auth_btn_status = false;
  msgstrategy_config_btn = false;

  @ViewChild('st', { static: false }) st: STComponent;
  columns: STColumn[] = [
    { title: '名称', index: 'msgName' },
    {
      title: '类型',
      index: 'msgType',
      type: 'tag',
      tag: BADGE_MSGTYPE,
      filter: {
        menus: [
          { text: '系统', value: 'SYS' },
          { text: '用户', value: 'USER' },
        ],
        multiple: false,
      },
    },
    { title: '子类型', index: 'sysType' },
    { title: '编码', index: 'msgCode' },
    { title: '备注', index: 'msgRemark' },
    { title: '状态', index: 'isFreeze', type: 'badge', badge: BADGE_STATUS },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          type: 'static',
          modal: { component: MsgMetaModalComponent, paramsName: 'record' },
          click: 'reload',
          iif: (item: STColumn) => {
            return this.auth_btn_edit;
          },
        },
        {
          text: record => (record.isFreeze === '1' ? `启用` : `禁用`),
          type: 'static',
          click: (record, _modal, comp) => {
            this.http
              .post(`/cf/admin/web-msg/meta/status`, { id: record.id, isFreeze: record.isFreeze })
              .subscribe(res => {
                if (res.status === 200) {
                  this.notice.success('成功', `成功${record.isFreeze === '1' ? '启用' : '禁用'}【${record.msgName}】`);
                  comp!.load();
                }
              });
          },
          iif: (item: STColumn) => {
            return this.auth_btn_status;
          },
        },
        {
          text: '更多',
          children: [
            {
              text: '配置接收者',
              icon: 'setting',
              type: 'static',
              modal: { component: StragegySelectModalComponent, paramsName: 'record' },
              iif: (item: STColumn) => {
                return item.msgType === 'SYS' && this.msgstrategy_config_btn;
              },
            },
            {
              text: '删除',
              type: 'del',
              icon: 'delete',
              pop: {
                title: '确定删除？',
                okType: 'danger',
              },
              click: (record, _modal, comp) => {
                this.http.delete(`/cf/admin/web-msg/meta/${record.id}`).subscribe(res => {
                  if (res.status === 200) {
                    this.notice.success('成功', `成功删除【${record.msgName}】`);
                    comp!.load();
                  }
                });
              },
              iif: (item: STColumn) => {
                return this.auth_btn_del;
              },
            },
          ],
        },
      ],
    },
  ];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private notice: NzNotificationService,
    private settings: SettingsService,
  ) {}

  ngOnInit() {
    this.auth_btn_add = this.settings.user.permissions.msgmeta_add_btn;
    this.auth_btn_edit = this.settings.user.permissions.msgmeta_edit_btn;
    this.auth_btn_del = this.settings.user.permissions.msgmeta_del_btn;
    this.auth_btn_status = this.settings.user.permissions.msgmeta_status_btn;
    this.msgstrategy_config_btn = this.settings.user.permissions.msgstrategy_config_btn;
  }

  add() {
    this.modal.createStatic(MsgMetaModalComponent, { i: { id: 0 } }).subscribe(() => this.st.reload());
  }
}
